package com.ruoyi.web.controller.fx;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.dto.FxUserDto;
import com.ruoyi.system.domain.fx.FxUser;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.fx.IFxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户Controller
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@RestController
@RequestMapping("/fx/user")
public class FxUserController extends BaseController {
    @Autowired
    private IFxUserService fxUserService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FxUser fxUser) {
//        startPage();
        List<FxUser> list = fxUserService.selectFxUserList(fxUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxUser fxUser) {
        List<FxUser> list = fxUserService.selectFxUserList(fxUser);
        ExcelUtil<FxUser> util = new ExcelUtil<FxUser>(FxUser.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fxUserService.selectFxUserById(id));
    }

    /**
     * 小程序获取用户详情接口
     * @param fxUser
     * @return
     */
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo(FxUser fxUser)
    {
//        fxUser.setSysUserid(getUserId());
        return success(fxUserService.selectFxUserByUserId(SecurityUtils.getUserId()));
    }

    /**
     * 新增用户
     */
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FxUser fxUser) {
        return toAjax(fxUserService.insertUserWithoutOne(fxUser));
    }

    /**
     * 门店用户添加（店铺新增/供应商新增）
     * @param fxUser
     * @return
     */
    @PostMapping("/userAdd")
    public AjaxResult userAdd(@RequestBody FxUser fxUser){
        return toAjax(fxUserService.insertUserInfo(fxUser));
    }

    /**
     * 门店用户修改（店铺新增/供应商新增）
     * @param fxUser
     * @return
     */
    @PutMapping("/userUpdate")
    public AjaxResult userUpdate(@RequestBody FxUser fxUser){
        return toAjax(fxUserService.userUpdate(fxUser));
    }


    /**
     *  门店用户添加
     * @return
     */
    @PostMapping("/storeUserAdd")
    public AjaxResult storeUserAdd(@RequestBody FxUser fxUser){
        return toAjax(fxUserService.insertStoreUser(fxUser));
    }

    /**
     * 小程序新增用户
     * @return
     */
    @Log(title = "小程序成为商家 和 供应商", businessType = BusinessType.INSERT)
//    @PostMapping("/addApp")
    public AjaxResult addApp(@RequestBody FxUserDto fxUserDto){
        return toAjax(fxUserService.insertUser(fxUserDto));
    }

    /**
     * 修改用户
     */
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxUser fxUser) {
        if (!fxUserService.checkUserNameUnique(fxUser))
        {
            return error("修改用户'" + fxUser.getUserName() + "'失败，用户名称已存在！");
        }
        return toAjax(fxUserService.updateFxUser(fxUser));
    }

    // 绑定分销用户
    @Log(title = "成为分销用户", businessType = BusinessType.UPDATE)
    @PostMapping("/becomeDistributeMember")
    public AjaxResult becomeDistributeMember(@RequestBody FxUser fxUser) {
//        FxUser fxUser1=new FxUser();
//        fxUser1.setPhoneNumber(fxUser.getPhoneNumber());
//        List<FxUser> users =  fxUserService.selectFxUserList(fxUser1);
//        FxUser   fxUserResult=users.get(0);
//        fxUserResult.setStoreId(fxUser.getStoreId());
        Long userId = SecurityUtils.getUserId();
        FxUser fxUserResult = fxUserService.selectFxUserByUserId(userId);
        if (StringUtils.isNull(fxUserResult.getDistributionId())) {
            fxUserResult.setDistributionId(fxUser.getStoreId());
        }
        System.out.println("---------------分销测试---"+ fxUser.getStoreId() +"----------");
        return toAjax(fxUserService.updateFxUser(fxUserResult));
    }

    /**
     * 删除用户
     */
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fxUserService.deleteFxUserByIds(ids));
    }

    /**
     * 修改用户（小程序）
     * @param fxUser
     * @return
     */
    @PutMapping("/edit")
    public AjaxResult editApp(@RequestBody FxUser fxUser) {
        fxUser.setSysUserid(getUserId());
        return toAjax(fxUserService.updateUser(fxUser));
    }


    /**
     * 修改用户
     */
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody FxUser fxUser) {
        FxUser fxUser1= fxUserService.selectFxUserById(fxUser.getId());
        SysUser sysUser =userService.selectUserById(fxUser1.getSysUserid());
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        return toAjax(userService.resetPwd(sysUser));
    }


    /**
     * 供应商新增账号
     */
    @PostMapping("/supplier")
    public AjaxResult supplierAdd(@RequestBody FxUser fxUser) {
        return toAjax(fxUserService.insertSupplierUserWithoutOne(fxUser));
    }

    /**
     * 供应商根据父id来查询多少个子账号
     */
    @GetMapping("/parentAccount")
    public AjaxResult supplierAdd(Long parentAccountId) {
        return success(fxUserService.selectSupplier(parentAccountId));
    }


}
